#ifndef TPCM_SRC_TPCM_CONSTANTS_H_
#define TPCM_SRC_TPCM_CONSTANTS_H_




/*TCM Command Ordinals: must less than TCM_ORDINALS_MAX*/
#define TPCM_ORD_START							  0x00001001
#define TPCM_ORD_BootMeasure                      0x00001001
#define TPCM_ORD_InterceptMeasure                 0x00001002
#define TPCM_ORD_CollectAndMeasure                0x00001003
#define TPCM_ORD_SetDynamicMeasurePolicy          0x00001004
#define TPCM_ORD_UpdateReference                  0x00001005
#define TPCM_ORD_UpdateReferenceIncrement         0x00001006
#define TPCM_ORD_GetTrustedCredential             0x00001007
#define TPCM_ORD_GetBootMeasureReference          0x00001008
#define TPCM_ORD_MeasureSwitch                    0x00001009
#define TPCM_ORD_GetTPCMStatus                    0x0000100A
#define TPCM_ORD_ImageUpdate					  0x0000100B
#define TPCM_ORD_SecureMemoryPolicyUpdate		  0x0000100C
#define TPCM_ORD_SecureMemoryPolicySwitch      	  0x0000100D
#define TPCM_ORD_GetTPCMLog                       0x0000100E
#define TPCM_ORD_FlashRead                        0x0000100F
#define TPCM_ORD_FlashWrite                       0x00001010
#define TPCM_ORD_FlashErase                       0x00001011
#define TPCM_ORD_SetSystemTime                    0x00001012
#define TPCM_ORD_GenerateLicenseReq               0x00001013
#define TPCM_ORD_LoadLicense                      0x00001014
#define TPCM_ORD_GetLicenseStatus                 0x00001015
#define TPCM_ORD_SIMBootMeasure					  0x00001016
#define TPCM_ORD_SetPlatformPik					  0x0000101B
#define TPCM_ORD_SetTpcmPikPubkey				  0x0000101C
#define TPCM_ORD_GetTpcmPikPubkey				  0x0000101D
#define TPCM_ORD_END							  0x0000101E

#define TPCM_TAG_RQU_COMMAND             0x000000C1 /* A command with no authentication.  */
#define TPCM_TAG_RSP_COMMAND             0x000000C4 /* A response from a command with no authentication*/

#define TPCM_TAG_RSP_IO_FAULT             0x000000C6

#ifndef TPCM_BUFFER_MAX
#define TPCM_BUFFER_MAX  0x1000  /* 4k bytes */
#endif

#define TPCM_SM3_MAXNUMBYTES  (TCM_BUFFER_MAX-64 )
#define TPCM_SM3_MAX          0x100000
#define TPCM_SM3_BUFFER          0x800




#define  TPCM_BOOT_CONTROL          0x53516331
#define  TPCM_MEASURE_NO_CONTROL    0x00000000
#define  TPCM_CONTROL_NO_EXEC       0x00010000


//referencelib  type
#define  TPCM_REFERENCE_STATIC      0x00000001
#define  TPCM_REFERENCE_DYNAMIC     0x00000002

//baseline update type

#define  TPCM_BASELINE_ADD      	0x00000000
#define  TPCM_BASELINE_UPDATE     	0x00000001
#define  TPCM_BASELINE_DELETE     	0x00000002


#define  TPCM_BOOT_UNKNOWN       	0x00000002
#define  TPCM_BOOT_ERROR         	0x00000001
#define  TPCM_BOOT_SUCCESS       	0x00000000

#define  TPCM_PATH_MAX        		512
#define  TPCM_ID_SIZE         		32


#define  TPCM_WHITELIST_ADD      	0x00000000
#define  TPCM_WHITELIST_UPDATE     	0x00000001
#define  TPCM_WHITELIST_DELETE     	0x00000002

#define  TPCM_WHITELIST_CACHE_NOT_FULL_NUM_INVALID    0xFFFFFFFF



#define DEAULT_HASH_SIZE 	32

#define MAX_CLIENT_ID_SIZE 	32
#define MAX_TPCM_ID_SIZE 	32

#ifndef MAX_HOST_ID_SIZE
#define MAX_HOST_ID_SIZE 	32
#endif
#define MAX_EK_PUBKEY_SIZE 	64
#define MAX_REMOTE_CERT_SIZE 	4096



#define DEAULT_PCR_SIZE 	32
#define DEAULT_SIGNATURE_SIZE 64

#define MAX_KEY_NAME_SIZE 	128
#define MAX_NV_NAME_SIZE 	256


#define AUTH_MAX_LEN 		128


#define TPCM_SM4_KEY_LENGTH  16
#define TPCM_SM4_IV_LENGTH   16

#define	TPCM_SIGNATURE_SIZE  64

#define TPCM_DIGEST_SIZE 	 32
typedef uint8_t TPCM_DIGEST[TPCM_DIGEST_SIZE];

typedef TPCM_DIGEST  TPCM_HMAC ;

#define TPCM_SECRET_SIZE 	 32
typedef uint8_t TPCM_SECRET[TPCM_SECRET_SIZE];
#define TPCM_HMAC_BLOCK_SIZE 64
#define TPCM_AUTHDATA_SIZE 	 32



enum  TPCM_IMEASURE_TYPE{
 TPCM_IMEASURE_TYPE_ilExecCount=1,
 TPCM_IMEASURE_TYPE_ilDLLLoadCount,
 TPCM_IMEASURE_TYPE_ilKernelModuleLoadCount,
 TPCM_IMEASURE_TYPE_ilFileAccessCount,
 TPCM_IMEASURE_TYPE_ilDeviceAccessCount,
 TPCM_IMEASURE_TYPE_ilNetworkVisitCount,
 TPCM_IMEASURE_TYPE_ilNetworkRequestCount,
 TPCM_IMEASURE_TYPE_measureCodeFailCount,
 TPCM_IMEASURE_TYPE_measureKernelCodeFailCount,
 TPCM_IMEASURE_TYPE_measureKernelDataFailCount,
 TPCM_BMEASURE_TYPE_ilbmeasure,
 TPCM_IMEASURE_TYPE_ilNoteFailCount,
 TPCM_IMEASURE_TYPE_MAX
};

enum  TPCM_MSG_TYPE{
 TPCM_MSG_TYPE_DMEASURE_NOTIFY=1,
 TPCM_MSG_TYPE_BMEASURE_NOTIFY,
 TPCM_MSG_TYPE_MAX
};


enum  TPCM_OP_TYPE{
	TPCM_OP_TYPE_BMEASURE=1,
	TPCM_OP_TYPE_DMEASURE,
	TPCM_OP_TYPE_MAX
};

enum  TPCM_OP_VALUE{
	TPCM_OP_CLOSE =0,
	TPCM_OP_OPEN =1,
	TPCM_OP_MAX
};

enum  TPCM_LOG_TYPE{
	TPCM_LOG_TYPE_BMEASURE =1,
	TPCM_LOG_TYPE_DMEASURE =2,
	TPCM_LOG_TYPE_MAX
};


enum  TPCM_ALG_TYPE{
	TPCM_ALG_TYPE_SOFT =1,
	TPCM_ALG_TYPE_HARD =2,
	TPCM_ALG_TYPE_MAX
};

enum  TPCM_STATUS_TYPE{
 TPCM_STATUS_TYPE_CMDS_PROCESSED=1,
 TPCM_STATUS_TYPE_CMDS_WAIT,
 TPCM_STATUS_TYPE_CMDS_PARA_ERR,
 TPCM_STATUS_TYPE_CMDS_REFUSED,
 TPCM_STATUS_TYPE_DMEASURE_TIMES,
 TPCM_STATUS_TYPE_BMEASURE_TIMES,
 TPCM_STATUS_TYPE_NOTIFY_TIMES,
 TMCM_STATUS_TYPE_STATUS_UNKNOWN,	//0x08
 TMCM_STATUS_TYPE_STATUS_UNSAFE,
 TMCM_STATUS_TYPE_STATUS_SAFE,
 TPCM_STATUS_TYPE_CPU_VERSION,
 TPCM_STATUS_TYPE_BIOS_VERSION,
 TPCM_STATUS_TYPE_MAX
};
 
enum  TPCM_LICENSE_STATUS{
	TPCM_LICENSE_STATUS_UNAUTHORIZED =0,
	TPCM_LICENSE_STATUS_AUTHORIZED =1,
	TPCM_LICENSE_STATUS_UNSUPPORTED,
};

enum{
	PCR_TYPE_NO = 0,
	PCR_TYPE_BIOS,        	//pcr1
	PCR_TYPE_BOOTLOADER,  	//pcr2
	PCR_TYPE_KERNAL,      	//pcr3
	PCR_TYPE_TSB,         	//pcr4  have deleted
	PCR_TYPE_BOOT_ALL,    	//pcr5
	PCR_TYPE_DMEAUSE_STATE, //pcr6	
	PCR_TYPE_APP_LOAD_STATE, //pcr7
	
	PCR_MAX,
};


typedef enum {
	ACTION_SET= 0 ,
	ACTION_ADD =1 ,
	ACTION_DELETE =2,
	ACTION_MODIFY=3,
	ACTION_MAX
} tpcm_action;

struct tpcm_auth_para
{
    uint32_t auth_type;
    uint32_t auth_length;
    uint8_t *auth;
    uint32_t uid_len;
    uint8_t *uid;
    uint32_t input_len;
    const uint8_t *input;
    uint32_t hash_len;
    uint8_t hash[DEAULT_HASH_SIZE];
};


/*TPCM CHECK STATUS*/
#define TPCM_CHECK_LICENSE_AUTHORIZED          0x00000001
#define TCM_CHECK_LICENSE_UNAUTHORIZED         0x00000004



#define TPCM_PERMANENT_START_ADDR 		CONFIG_TPCM_NORFLASH_START_ADDR
//TPCM_PERMANENT_DATA_BAK
//#define TPCM_PERMANENT_BAK_OFFSET  CONFIG_TPCM_PERMANENT_STRUCTURE_SIZE
#define TPCM_PERMANENT_START_ADDR_BAK 	(TPCM_PERMANENT_START_ADDR 		+ 	CONFIG_TPCM_PERMANENT_STRUCTURE_SIZE)
#define TPCM_ALL_PERMANENT_START_ADDR	(TPCM_PERMANENT_START_ADDR_BAK + CONFIG_TPCM_PERMANENT_STRUCTURE_SIZE)

#define	TPCM_SLEEP_MODE_DATA_START_ADDR	(TPCM_ALL_PERMANENT_START_ADDR + CONFIG_TPCM_SLEEP_DATA_ALL_STORE_SIZE)
//#define TPCM_WHITELIST_HEAD_ADDR      	(TPCM_PERMANENT_START_ADDR_BAK	+ 	CONFIG_TPCM_PERMANENT_STRUCTURE_SIZE)
//#define TPCM_WHITELIST_HEAD_BAK_ADDR    (TPCM_WHITELIST_HEAD_ADDR		+	CONFIG_TPCM_WHITELIST_HEAD_SIZE)
#define TPCM_WHITELIST_DATA_START_ADDR  (TPCM_SLEEP_MODE_DATA_START_ADDR + CONFIG_TPCM_SLEEP_DATA_SIZE)
#define TPCM_WHITELIST_END_ADDR      	CONFIG_TPCM_NORFLASH_END_ADDR

#ifndef CONFIG_TPCM_HEAP_SIZE
#define CONFIG_TPCM_HEAP_SIZE (128*1024)
#endif

#define CONFIG_TPCM_UNIT_HEAP_SIZE         (CONFIG_TPCM_HEAP_SIZE / 3)
#define TPCM_AVAILABLE_MEM_SPACE_LEN 	   (CONFIG_TPCM_PERMANENT_STRUCTURE_SIZE > CONFIG_TPCM_UNIT_HEAP_SIZE ? CONFIG_TPCM_UNIT_HEAP_SIZE : CONFIG_TPCM_PERMANENT_STRUCTURE_SIZE)
#define TPCM_AVAILABLE_MEM_SPACE_BASE      (64)
#define TPCM_AVAILABLE_MEM_SPACE_MULTIPLE  (TPCM_AVAILABLE_MEM_SPACE_LEN / TPCM_AVAILABLE_MEM_SPACE_BASE)

enum
{
	CONTEXT_ENV_MEM_OCCUPY_LEN = 108,/*bytes*/
	CONTEXT_DMEASURE_MEM_OCCUPY_LEN = 2*TPCM_AVAILABLE_MEM_SPACE_MULTIPLE,/*bytes*/
	CONTEXT_IMEASURE_MEM_OCCUPY_LEN = 48,/*bytes*/
	CONTEXT_WHITE_LIST_MEM_OCCUPY_LEN = 84,/*bytes*/
	CONTEXT_BOOT_MEASURE_MEM_OCCUPY_LEN = 16*TPCM_AVAILABLE_MEM_SPACE_MULTIPLE,/*bytes*/
	CONTEXT_LICENSE_MEM_OCCUPY_LEN = 247, /*bytes*/
	CONTEXT_KEY_MANAGE_MEM_OCCUPY_LEN = 84,/*bytes*/
	CONTEXT_LOG_MEM_OCCUPY_LEN = 68,/*bytes*/
	CONTEXT_WORK_MANAGE_MEM_OCCUPY_LEN = 32,/*bytes*/
#if CONFIG_SUPPORT_PROCESS_IDENTITY
	CONTEXT_PROCESS_IDENTITY_MEM_OCCUPY_LEN = 8*TPCM_AVAILABLE_MEM_SPACE_MULTIPLE,/*bytes*/
#endif
#if CONFIG_SUPPORT_PROCESS_ROLES
	CONTEXT_PROCESS_ROLES_MEM_OCCUPY_LEN = 8*TPCM_AVAILABLE_MEM_SPACE_MULTIPLE,/*bytes*/
#endif
#if CONFIG_SUPPORT_SHELL
	CONTEXT_SHELL_MEM_OCCUPY_LEN = 68,/*bytes*/
#endif
	CONTEXT_CERT_ADMIN_MEM_OCCUPY_LEN = 2*TPCM_AVAILABLE_MEM_SPACE_MULTIPLE,/*bytes*/
	CONTEXT_AUTH_POLICY_MEM_OCCUPY_LEN = 2*TPCM_AVAILABLE_MEM_SPACE_MULTIPLE,/*bytes*/
	CONTEXT_GLOBALPOLICY_MANAGE_MEM_OCCUPY_LEN = 160,/*bytes*/
	CONTEXT_STATUS_MEM_OCCUPY_LENO = 64,/*bytes*/
	CONTEXT_VOLATILE_DATA_MEM_OCCUPY_LEN = 4*TPCM_AVAILABLE_MEM_SPACE_MULTIPLE,/*bytes*/
#if CONFIG_SUPPORT_DMEASURE_PROCESS
	CONTEXT_DMEASURE_PROCESS_MEM_OCCUPY_LEN = 12*TPCM_AVAILABLE_MEM_SPACE_MULTIPLE,/*bytes*/
#endif
	CONTEXT_PTRACE_MEM_OCCUPY_LEN = 4*TPCM_AVAILABLE_MEM_SPACE_MULTIPLE,  /*bytes*/
#if CONFIG_SUPPORT_TRUSTED_LINK_POLICY
	CONTEXT_TRUSTED_LINK_MEM_OCCUPY_LEN = 1*TPCM_AVAILABLE_MEM_SPACE_MULTIPLE,/*bytes*/
#endif

	CONTEXT_VTCM_MEM_OCCUPY_LEN = 1*TPCM_AVAILABLE_MEM_SPACE_MULTIPLE,/*bytes*/
	CONTEXT_CRITICAL_FILE_REF_MEM_OCCUPY_LEN = 80,/*bytes Currently only stored integrity hash*/

	CONTEXT_OTHER_MEM_OCCUPY_LEN = 4*TPCM_AVAILABLE_MEM_SPACE_MULTIPLE,/*bytes*/
};


#define roundup(x,y)     ((((x) + (y) - 1) / (y)) * (y))
#define rounddown(x,y)   (((x) / (y)) * (y))


#endif /* TPCM_SRC_TPCM_CONSTANTS_H_ */
